//
//  LDYComposeTextView.swift
//  李东阳微博
//
//  Created by 李东阳 on 16/4/23.
//  Copyright © 2016年 李东阳. All rights reserved.
//

import UIKit

class LDYComposeTextView: UITextView {

    override var font :UIFont?{
    
        didSet{
        
        placeholderLabel.font = font
        }
    
    }
    
    var placeHolder:String?{
    
        didSet{
        
        placeholderLabel.text = placeHolder
        
        }
    
    }
    
    
    override init(frame: CGRect, textContainer: NSTextContainer?) {
        super.init(frame: frame, textContainer: textContainer)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    private func setupUI(){
        
    self.backgroundColor = UIColor.whiteColor()
    //添加控件
        addSubview(placeholderLabel)
        
      // 设置约束
        
      placeholderLabel.snp_makeConstraints { (make) -> Void in
        make.leading.equalTo(5)
        make.top.equalTo(8)
        make.width.equalTo(UIScreeonwidth)
        }
        
       NSNotificationCenter.defaultCenter().addObserver(self, selector: "textViewTextDidChange", name: UITextViewTextDidChangeNotification, object: nil)
    }
    
    
    //MARK: - 监听方法
      @objc private func textViewTextDidChange(){
    
       placeholderLabel.hidden = self.hasText()
    
    }
    
    
    deinit{
    
    NSNotificationCenter.defaultCenter().removeObserver(self)
    }
    
    private lazy var placeholderLabel: UILabel = {
    
let lab = UILabel(textColor: UIColor.grayColor(), textFont: 14)
       
     lab.numberOfLines = 0
     return lab
    
    }()
    
    
    
}
